package ch.evaleto.gwt.media.client.player;

import java.util.ArrayList;
import java.util.List;


import ch.evaleto.gwt.media.client.player.medias.Podcast;
import ch.evaleto.gwt.media.client.player.medias.Radio;
import ch.evaleto.gwt.media.client.player.medias.Sound;
import ch.evaleto.gwt.media.client.player.medias.Video;
import ch.evaleto.gwt.media.client.player.util.EventListenerList;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONException;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Window;

public class Playlist {
	final List<Media> medias=new ArrayList<Media>();
	private EventListenerList listeners;
	
	private int current;
	
	public Playlist() {
		current=0;
	}
	
	public void add(Playlist list){
		medias.addAll(list.medias);
	}
	
	public void add(Media media){
		medias.add(media);
	}
	
	public void remove(Media media){
		medias.remove(media);	
	}

	public void remove(int media){
		if (current>=medias.size())
			current=0;
		medias.remove(media);	
	}
	
	public void removeAll(){
		medias.clear();	
	}
	
	public Media current(){
		return medias.get(current);
	}
	
	public Media next(){
		if (current>=medias.size())
			current=0;
		else 
			current++;
		return current();
	}
	public Media back(){
		if (current==0)
			current=medias.size()-1;
		else 
			current--;
		return medias.get(current);
	}
	
	public boolean isEmpty(){
		return medias.size()==0;
	}	
	
	public List<Media> list(){
		return medias;
	}

	/**
	 * json factory
	 * @param content
	 * @return
	 */
	public static Playlist fromJson(String content){
	  try {
            // parse the response text into JSON
            JSONValue jsonValue = JSONParser.parse(content);
            JSONArray jsonArray = jsonValue.isArray();
            
            if (jsonArray == null) {
              throw new JSONException(); 
            }
            
            Playlist q=new Playlist();
            
            
            for (int i=0;i<jsonArray.size();i++){
            	String id=jsonArray.get(i).isObject().get("id").isString().stringValue();
            	String media=jsonArray.get(i).isObject().get("media").isString().stringValue();
            	String name=jsonArray.get(i).isObject().get("name").isString().stringValue();
            	String url=jsonArray.get(i).isObject().get("href").isString().stringValue();
            	String comment=jsonArray.get(i).isObject().get("html").isString().stringValue();
            	String image=jsonArray.get(i).isObject().get("image").isString().stringValue();
            	String tags=jsonArray.get(i).isObject().get("tags").isString().stringValue();
            	String vote=jsonArray.get(i).isObject().get("vote").isString().stringValue();
            	String love=jsonArray.get(i).isObject().get("love").isString().stringValue();
            	String schedule=jsonArray.get(i).isObject().get("schedule").isString().stringValue();
            	if (media.equalsIgnoreCase("radio"))
            		q.add(new Radio(url, name, id, "-1", schedule, tags, vote, love, comment, image));
            	else
            	if (media.equalsIgnoreCase("podcast"))
            		q.add(new Podcast(url, name, id, "-1", schedule, tags, vote, love, comment, image));
            	else
            	if (media.equalsIgnoreCase("video"))
            		q.add(new Video(url, name, id, "-1", schedule, tags, vote, love, comment, image));
            	else
            		q.add(new Sound(url, name, id, "-1", schedule, tags, vote, love, comment, image));
            }
            return q;
          } catch (JSONException e) {
            Window.alert("Could not parse JSON");
          }
		
		return new Playlist();
	}
	/**
     * Format of playlist (.pls) files is:
     * ----------------------
     * [playlist]
     * NumberOfEntries=3
     * Version2
     * File1=http://url.com
     * Tittle1=
     * Lenght1=-1
     * 
     * File2=http://url.com
     * Tittle2=
     * Length2=-1
	 * @throws QueueException 
     */
	
//	public static Queue fromPLS(final String url) throws QueueException{
//
//	public static Queue loadfromPLS(String url, String content) throws QueueException{
//		// detect .pls
//		// http://fr.wikipedia.org/wiki/PLS_(format_de_fichier)
//		if (content.indexOf("[playlist]")==-1){
//			throw new QueueException("error reading ["+url+"] it is not a PLS format");
//		}
//		HashMap<String, String> pls=new HashMap<String, String>();
//		
//		for (String line:content.split("\n")){
//			if (line.indexOf('=')==-1)
//				continue;
//			String entry[]=line.split("=");
//			pls.put(entry[0].toLowerCase(), (entry.length>1)?entry[1]:"");
//		}
//		//create a queue
//		Queue q=new Queue();
//		for(int i=0;i<(pls.keySet().size()/3);i++){
//			String f=pls.get("file"+i);
//			String t=pls.get("tittle"+i);
//			String l=pls.get("length"+i);
//			if (f==null || f.equals(""))
//				throw new QueueException("error file for entry["+i+"] is Null");
//			String id[]=f.split("/");
//			//stream case
//			if (l==null || l.equals("-1") || l.equals("")){
//				q.add(new Radio(f,t,id[id.length-1],l, "","","",""));
//				continue;
//			}
//			q.add(new Sound(f,t,id[id.length-1],l,"","","",""));
//		}
//			
//		return null;
//	}
	
}
